String.prototype.startWith = function (str) {
    var reg = new RegExp("^" + str);
    return reg.test(this);
};

String.prototype.endWith = function (str) {
    var reg = new RegExp(str + "$");
    return reg.test(this);
};
var persistence = (function () {
    var namespace = "_";
    var listeners = [];
    return {
        setNamespace: function (_namespace) {
            namespace = _namespace+"_";
        },
        addListener: function (listener) {
            listeners.push(listener);
        },
        put: function (key, value) {
            var obj = value;
            if (value instanceof Object) {
                value = JSON.stringify(value);
            }
            localStorage[namespace + key] = value;
            for (var i = 0; i < listeners.length; i++) {
                var listener = listeners[i];
                if (listener.key == key) {
                    listener.onDataChange(obj);
                }
            }
        },
        get: function (key) {
            var r = localStorage[namespace + key];
            try {
                r = JSON.parse(r);
            } catch (e) {
            }
            return r;
        },
        getAll: function () {
            var r = [];
            for (var key in localStorage) {
                if (key.startWith(namespace)) {
                    r.push(localStorage[key]);
                }
            }
            return r;
        },
        remove: function (key) {
            localStorage.removeItem(namespace + key);
        }
    };
})();